var BORDER_LAYER_DEPTH:Number=-1;

var displayMode:Object;
var container:MovieClip;
var headerCount:Number;

// style parameters
// layout factor | layout:Object
// mainLocation:{x,y,width,height}
// subLocation:{bx,by,width,height,dx,dy}
var styleParams:Object;
var oldLayout:Object;

function initialize(comp:MovieClip, noMask:Boolean, noBorder:Boolean, noCover:Boolean)
{
	container=comp;
	if (container.border!=null) return;		// ignore now if existed
	initializeSubElements(noMask, noBorder, noCover);
}

function initializeSubElements(noMask:Boolean, noBorder:Boolean, noCover:Boolean)
{
	var subElements:Array=new Array();

	for (var a in container)
	{
		if (typeof(container[a])=="movieclip")
		{
			if (container[a]._name.indexOf("#")==-1)
			{
				subElements.push(container[a]);
			}
		}
	}

	for (var i=0; i<subElements.length; i++)
	{
		// Packages.log(i+": "+subElements[i], "debug");
		if (!noMask) generateMask(subElements[i]);
		if (!noBorder) generateBorder(subElements[i]);
		if (!noCover) generateCoverBorder(subElements[i]);
	}
}

function generateCoverBorder(target:MovieClip)
{
	for (var a in target)
	{
		if (typeof(target[a])=="movieclip" && a.indexOf("Cover")>-1)
		{
			var container=target[a];
			var width=container._width;
			var height=container._height;
			var border:MovieClip=container.createEmptyMovieClip("border", BORDER_LAYER_DEPTH);
			border.attachMovie("border_l320_1.png", "line_t", border.getNextHighestDepth(), {_width:width});
			border.attachMovie("border_l320_2.png", "line_b", border.getNextHighestDepth(), {_y:height, _width:width});
			border.attachMovie("border_l80_1.png", "line_l", border.getNextHighestDepth(), {_height:height});
			border.attachMovie("border_l80_2.png", "line_r", border.getNextHighestDepth(), {_x:width, _height:height});
			border._alpha=60;
		}
	}
}

function generateMask(target:MovieClip)
{
	var target_mask:MovieClip=container.createEmptyMovieClip(target._name+"_mask", container.getNextHighestDepth());
	drawTransparentRect(target_mask, target._width, target._height);
	target_mask._x=target._x;
	target_mask._y=target._y-10;
	target.setMask(target_mask);
	// Packages.log("target: "+target.getDepth(), "debug");
	// Packages.log("target_mask: "+target_mask.getDepth(), "debug");
}

/*function generateBorder(target:MovieClip)
{
	var border:MovieClip=target.attachMovie("frame", "border", target.getNextHightestDepth());
	resize(target, target._width, target._height-10);
}

function resize(target:MovieClip, width:Number, height:Number)
{
	container[target._name+"_mask"]._width=width;
	container[target._name+"_mask"]._height=height+10;

	var border:MovieClip=target.border;
	border._width=width;
	border._height=height;
}*/

function generateBorder(target:MovieClip)
{
	// set as default
	var height=target._height-10;
	var width=target._width;

	// to establish the border layer container
	var border:MovieClip=target.createEmptyMovieClip("border", BORDER_LAYER_DEPTH);

	// to attach related assets
	// TODO: attach the smart assets
	border.attachMovie("border_corner_lt.png", "corner_lt", border.getNextHighestDepth());
	border.attachMovie("border_corner_rt.png", "corner_rt", border.getNextHighestDepth());
	border.attachMovie("border_corner_lb.png", "corner_lb", border.getNextHighestDepth());
	border.attachMovie("border_corner_rb.png", "corner_rb", border.getNextHighestDepth());
	border.attachMovie("border_l320_1.png", "line_t", border.getNextHighestDepth());
	border.attachMovie("border_l320_2.png", "line_b", border.getNextHighestDepth());
	border.attachMovie("border_l80_1.png", "line_l", border.getNextHighestDepth());
	border.attachMovie("border_l80_2.png", "line_r", border.getNextHighestDepth());

	// to adjust the location and shape
	resize(target, width, height);
}

function resize(target:MovieClip, width:Number, height:Number)
{	
	// Packages.log(target._name+".resize: "+width+"|"+height, "debug");
	
	container[target._name+"_mask"]._width=width;
	container[target._name+"_mask"]._height=height+10;
	
	var border:MovieClip=target.border;

	border.corner_rt._x=width-border.corner_rt._width;
	border.corner_lb._y=height-border.corner_lb._height;
	border.corner_rb._x=width-border.corner_rb._width;
	border.corner_rb._y=height-border.corner_rb._height;

	border.line_t._x=border.corner_lt._width;
	border.line_t._width=width-border.corner_lt._width-border.corner_rt._width;
	border.line_b._x=border.line_t._x;
	border.line_b._y=height-border.line_b._height;
	border.line_b._width=border.line_t._width;
	border.line_l._y=border.corner_lt._height;
	if (height-border.corner_lt._height-border.corner_lb._height<0)
	{
		border.line_l._height=0;
	}
	else border.line_l._height=height-border.corner_lt._height-border.corner_lb._height;
	border.line_r._x=width-border.line_r._width;
	border.line_r._y=border.line_l._y;
	border.line_r._height=border.line_l._height;
}

function setExpandPortletArr(pArr:Array, styleObj:Object)
{
	// initialize elements display mode
	initializeDisplayMode(pArr);
	// set the style parameters
	styleParams=styleObj;
	// add header interaction
	// click the header to change the display mode(expand/normal/header)
	addHeaderLayer(pArr);
}

function initializeDisplayMode(pArr:Array)
{
	headerCount=0;
	oldLayout=new Object();
	displayMode=new Object();
	for (var i=0; i<pArr.length; i++)
	{
		displayMode[pArr[i]._name]="normal";
		oldLayout[pArr[i]._name]=new Object();
		oldLayout[pArr[i]._name].x=pArr[i]._x;
		oldLayout[pArr[i]._name].y=pArr[i]._y;
		oldLayout[pArr[i]._name].width=pArr[i]._width;
		oldLayout[pArr[i]._name].height=pArr[i]._height;
	}
}

function addHeaderLayer(pArr:Array)
{
	for (var i=0; i<pArr.length; i++)
	{
		var header;
		header=createHotSpotLayer(pArr[i]);
		addHeaderEvent(header);
	}
}

function addHeaderEvent(header:MovieClip)
{
	// header.onRollOver=onRollOverEvent;
	// header.onRollOut=onRollOutEvent;
	// header.onPress=onPressEvent;
	// header.onReleaseOutside=onRollOutEvent;
	header.onRelease=onReleaseEvent;
}

function onReleaseEvent()
{
	// Packages.log("onReleaseEvent: "+this);
	// change display mode
	changeDisplayMode(mlf.mcmanager.PathResolver.drawPath(this));
}

function invokeReleaseEvent(selectedItemPath:String)
{
	changeDisplayMode(selectedItemPath);
}

function changeDisplayMode(selectedItemStr:String)
{
	var itemStrArr=selectedItemStr.split("-");
	var selectedItemName=itemStrArr[itemStrArr.length-2];
	
	if (displayMode[selectedItemName]=="normal")
	{
		for (var a in displayMode)
		{
			displayMode[a]="header";
		}
		displayMode[selectedItemName]="expand";
	}
	else 
	{
		for (var a in displayMode)
		{
			displayMode[a]="normal";
		}
	}

	for (var a in displayMode)
	{
		if (displayMode[a]=="normal")
		{
			changeNormal(a);
		}
		else if (displayMode[a]=="expand")
		{
			changeExpand(a);
		}
		else if (displayMode[a]=="header")
		{
			changeHeader(a);
		}
	}

	// dispatch message
	Packages.system.mlf.GateKeeper.dispatchMessage({type:"onJAccodionCellEvent", param:selectedItemName, data:displayMode});
}

function changeNormal(pName:String)
{
	// sample code
	// Packages.log("changeNormal: "+pName);

	//-/ container[pName]._x=oldLayout[pName].x;
	container[pName+"_mask"]._x=oldLayout[pName].x;
	//-/ container[pName]._y=oldLayout[pName].y;
	container[pName+"_mask"]._y=oldLayout[pName].y-10;
	resize(container[pName], oldLayout[pName].width, oldLayout[pName].height-10);
	// container[pName]._width=oldLayout[pName].width;
	// container[pName]._height=oldLayout[pName].height;
	headerCount=0;

	for (var a in container[pName])
	{
		var selectedTarget=container[pName][a];
		if (typeof(selectedTarget)=="movieclip")
		{
			selectedTarget._visible=true;
		}
	}

	Packages.system.render.Animation.locate(container[pName], oldLayout[pName].x, oldLayout[pName].y);		//-/
}

function changeExpand(pName:String)
{
	// sample code
	// Packages.log("changeExpand: "+pName);
	
	//-/ container[pName]._x=styleParams.mainLocation.x;
	container[pName+"_mask"]._x=styleParams.mainLocation.x;
	//-/ container[pName]._y=styleParams.mainLocation.y;
	container[pName+"_mask"]._y=styleParams.mainLocation.y-10;
	resize(container[pName], styleParams.mainLocation.width, styleParams.mainLocation.height-10);
	// container[pName]._width=styleParams.mainLocation.width;
	// container[pName]._height=styleParams.mainLocation.height;
	Packages.system.render.Animation.locate(container[pName], styleParams.mainLocation.x, styleParams.mainLocation.y);		//-/
}

function changeHeader(pName:String)
{
	// sample code
	// Packages.log("changeHeader: "+pName);
	//-/ container[pName]._x=styleParams.subLocation.bx+styleParams.subLocation.dx*headerCount;
	container[pName+"_mask"]._x=styleParams.subLocation.bx+styleParams.subLocation.dx*headerCount;
	//-/ container[pName]._y=styleParams.subLocation.by+styleParams.subLocation.dy*headerCount;
	container[pName+"_mask"]._y=styleParams.subLocation.by+styleParams.subLocation.dy*headerCount-10;
	resize(container[pName], styleParams.subLocation.width, styleParams.subLocation.height-10);
	// container[pName]._width=styleParams.subLocation.width;
	// container[pName]._height=styleParams.subLocation.height;
	headerCount++;

	for (var a in container[pName])
	{
		var selectedTarget=container[pName][a];
		if (typeof(selectedTarget)=="movieclip")
		{
			if (selectedTarget._name!="border" && selectedTarget._name.indexOf("Header")==-1 && selectedTarget._name.indexOf("header")==-1)
			{
				selectedTarget._visible=false;
			}
		}
	}

	Packages.system.render.Animation.locate(container[pName], container[pName+"_mask"]._x, container[pName+"_mask"]._y+10);		//-/
}

function createHotSpotLayer(portlet:MovieClip):MovieClip
{
	var header=portlet.createEmptyMovieClip("header", portlet.getNextHighestDepth());
	
	header=drawTransparentRect(header, container._width, 20);

	header._y=-10;

	return header;
}

function drawTransparentRect(target:MovieClip, width:Number, height:Number):MovieClip
{
	target.moveTo(0, 0);

	target.beginFill(0x000000, 0);
	target.lineTo(width, 0);
	target.lineTo(width, height);
	target.lineTo(0, height);
	target.lineTo(0, 0);
	target.endFill();

	return target;
}